/**
 * Author: grx
 * Description: 用于学习模块"正向计时"的工具类
 */
package com.example.parallelandroid.utils;

import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;

import java.util.Timer;
import java.util.TimerTask;

public class ForwardTimeUtil {
    /*正向计时最大时长  单位：秒*/
    public static int MAX_TIME = 6 * 60 * 60 + 59 * 60;
    /*当前做*/
    private static int CURR_COUNT = 0;
    /*预计结束的时间*/
    private static long TIME_END = 0;
    /*计时器*/
    private static Timer forwardTimer;
    /*显示倒计时的textView*/
    private static TextView tvHour;
    private static TextView tvMinute;
    private static TextView tvSecond;

    private static Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            if (msg.what <= 0) {
                if (forwardTimer != null) {
                    forwardTimer.cancel();
                    forwardTimer = null;
                }
                /*ForwardTimeUtil.tvHour.setText("00");
                ForwardTimeUtil.tvMinute.setText("00");
                ForwardTimeUtil.tvSecond.setText("00");
                ForwardTimeUtil.tvHour.setEnabled(true);*/
                ForwardTimeUtil.tvMinute.setEnabled(true);
                ForwardTimeUtil.tvSecond.setEnabled(true);
            } else {
                //正向计时效果展示
                String txtTime = formatSecondTime(msg.what);
                String[] times = txtTime.split(":");
                String hour = times[0];
                String minute = times[1];
                String second = times[2];

                Log.e("时间", hour + " : " + minute + " : " + second);

                tvHour.setText("0" + hour);
                if (Integer.parseInt(minute) < 10) {
                    tvMinute.setText("0" + minute);
                } else {
                    tvMinute.setText(minute);
                }
                if (Integer.parseInt(second) < 10) {
                    tvSecond.setText("0" + second);
                } else {
                    tvSecond.setText(second);
                }
                ForwardTimeUtil.tvHour.setEnabled(true);
                ForwardTimeUtil.tvMinute.setEnabled(true);
                ForwardTimeUtil.tvSecond.setEnabled(true);
            }
            super.handleMessage(msg);
        }
    };

    /**
     * 开始倒计时
     * @param isFirst 标识是否是第一次进入
     * @param tvHour 显示正向计时的小时
     * @param tvMinute 显示正向计时的分钟
     * @param tvSecond 显示正向计时的秒
     */
    public static void startTimer(boolean isFirst, TextView tvHour, TextView tvMinute, TextView tvSecond) {
        long data = System.currentTimeMillis();
        // 第一次进入时，重新赋值
        if (isFirst) {
            CURR_COUNT = 1;
            TIME_END = MAX_TIME * 1000;
        } else {
            CURR_COUNT = ((int)data) / 1000;
        }
        //开始正向计时
        ForwardTimeUtil.tvHour = tvHour;
        ForwardTimeUtil.tvMinute = tvMinute;
        ForwardTimeUtil.tvSecond = tvSecond;
        if (forwardTimer == null) {
            forwardTimer = new Timer();
            forwardTimer.schedule(new TimerTask() {
                @Override
                public void run() {
                    Message msg = new Message();
                    msg.what = CURR_COUNT++;
                    handler.sendMessage(msg);
                }
            }, 0, 1000);
            //第二个参数delay:"0"的意思是:无延迟
            //第三个参数period:"1000"的意思是:每隔多长时间调用一次（单位毫秒）
        }
    }

    /**
     * 结束正向计时
     */
    public static void stopTimer() {
        //发送消息，结束正向计时
        Message message = new Message();
        message.what = 0;
        handler.sendMessage(message);
    }



    /**
     * 时间格式化方法
     * @param time 秒数
     * @return
     */
    private static String formatSecondTime(int time) {
        int hour = 0;
        int minute = 0;
        int second = 0;
        if (time >= 60) {
            minute = time / 60;   //取整
            second = time % 60;   //取余
        } else {
            second = time;
        }
        if (minute >= 60) {
            hour = minute / 60;
            minute = minute % 60;
        }
        String strtime = hour+":"+minute+":"+second;
        return strtime;
    }

}
